package com.team.teamproject.utils;

import com.alibaba.fastjson.JSON;
import com.team.teamproject.bean.Banners;
import com.team.teamproject.bean.Category_Channels;
import com.team.teamproject.bean.Category_Colums;
import com.team.teamproject.bean.Channels;
import com.team.teamproject.bean.Data_HomePageBottom;
import com.team.teamproject.bean.DetailsData;
import com.team.teamproject.bean.Mod_Homepage_Data;
import com.team.teamproject.bean.SearchData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/5.
 */

public class ParseJson {
//首页 标签
    public static List<Channels> fromTabLayoutTitle(String json) {
        List<Channels> list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(json);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONArray channels = data.getJSONArray("channels");
            list = JSON.parseArray(channels.toString(), Channels.class);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
//精选底部解析 和 其他标签解析
    public static List<Data_HomePageBottom> fromToHomeBottom(String json) {
        List<Data_HomePageBottom> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            JSONObject data = object.getJSONObject("data");
            Data_HomePageBottom datas = new Data_HomePageBottom();
            JSONArray items = data.getJSONArray("items");
            List<Data_HomePageBottom.ItemsBean> list1 = JSON.parseArray(items.toString(), Data_HomePageBottom.ItemsBean.class);
            datas.setItems(list1);
            list.add(datas);
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    //精选中间数据解析
    public static List<Mod_Homepage_Data> fromToHomeMod(String json) {
        List<Mod_Homepage_Data> dataList = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            JSONObject data = object.getJSONObject("data");
            JSONArray array = data.getJSONArray("secondary_banners");
            Mod_Homepage_Data modData = new Mod_Homepage_Data();
            List<Mod_Homepage_Data.SecondaryBannersBean> list = JSON.parseArray(array.toString(), Mod_Homepage_Data.SecondaryBannersBean.class);
            modData.setSecondary_banners(list);
            dataList.add(modData);
            return dataList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    //----------------------------
    public static List<Category_Channels> parseJsonToCategory(String json) {
        List<Category_Channels> category_channelsList;
        try {
            JSONObject object = new JSONObject(json);
            JSONObject object1 = object.optJSONObject("data");
            JSONArray array = object1.optJSONArray("channel_groups");
            category_channelsList = JSON.parseArray(array.toString(), Category_Channels.class);
            return category_channelsList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<Category_Colums> parseJsonToColums(String json) {
        List<Category_Colums> categoryColumses = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            JSONObject object1 = object.optJSONObject("data");
            JSONObject object2 = object1.optJSONObject("paging");
            Category_Colums colums = new Category_Colums();
            colums.setNext_url(object2.optString("next_url"));
            JSONArray array = object1.optJSONArray("columns");
            List<Category_Colums.ColumnsBean> columnsBeanList = JSON.parseArray(array.toString(), Category_Colums.ColumnsBean.class);
            colums.setColumns(columnsBeanList);
            categoryColumses.add(colums);
            return categoryColumses;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<DetailsData> fromToHomeXq(String json) {
        List<DetailsData> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            DetailsData data = new DetailsData();
            JSONObject object1 = object.getJSONObject("data");
            data.setContent_html(object1.optString("content_html"));
            list.add(data);
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    //搜索后详情条目的内容解析
    public static List<DetailsData> fromToSearchXq(String json) {
        List<DetailsData> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            DetailsData data = new DetailsData();
            JSONObject object1 = object.getJSONObject("data");
            data.setUrl(object1.optString("url"));
            list.add(data);
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<Banners> fromToHomeTop(String jsonTop) {
        List<Banners> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(jsonTop);
            JSONObject object1 = object.getJSONObject("data");
            Banners banners = new Banners();
            JSONArray array = object1.getJSONArray("banners");
            List<Banners.BannersBean> bannersBeen = JSON.parseArray(array.toString(), Banners.BannersBean.class);
            banners.setBanners(bannersBeen);
            list.add(banners);
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<SearchData> fromToSearchDetail(String json) {
        List<SearchData> list = new ArrayList<>();
        try {
            JSONObject object = new JSONObject(json);
            JSONObject data = object.getJSONObject("data");
            SearchData dataBean = new SearchData();
            JSONArray items = data.getJSONArray("items");
            List<SearchData.ItemsBean> itemsBeen = JSON.parseArray(items.toString(), SearchData.ItemsBean.class);
            dataBean.setItems(itemsBeen);
            list.add(dataBean);
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}